package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.MyGroupsBean;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

/**
 * Created by Jinni on 2017/9/20 0020.
 */

public class MyGroupsAdapter extends BaseRecyclerViewAdapter<MyGroupsBean.DataBean.NumberBean.AddBean> {

    private Context mContext;
    private OnItemClick mListener;

    public void setListener(OnItemClick listener) {
        mListener = listener;
    }

    public MyGroupsAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_my_groups, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            MyGroupsBean.DataBean.NumberBean.AddBean bean = mList.get(position);
            onBindData((ViewHolder) holder, bean);
            onBindListener((ViewHolder) holder, bean);
        }
    }

    private void onBindData(ViewHolder holder, MyGroupsBean.DataBean.NumberBean.AddBean bean) {
        String avatar = bean.getGroupphoto();
        String groupName = bean.getGroupname();
        RequestOptions requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .override(350, 350);
        Glide.with(mContext.getApplicationContext())
                .load(TextUtils.isEmpty(avatar) ? R.mipmap.default_avatar : HttpRequestConstant.GROUP_PIC_HEAD + avatar)
                .apply(requestOptions)
                .into(holder.mCivGroupAvatar);
        holder.mTvGroupName.setText(groupName);
    }

    private void onBindListener(final ViewHolder holder, final MyGroupsBean.DataBean.NumberBean.AddBean bean) {
        holder.mItemView.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                mListener.onItemClickListener(holder.getAdapterPosition(), bean);
            }
        });
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        CircleImageView mCivGroupAvatar;
        TextView mTvGroupName;
        View mItemView;

        ViewHolder(View itemView) {
            super(itemView);
            mItemView = itemView;
            mCivGroupAvatar = (CircleImageView) itemView.findViewById(R.id.civ_group_avatar);
            mTvGroupName = (TextView) itemView.findViewById(R.id.tv_group_name);
        }
    }

    public interface OnItemClick {
        void onItemClickListener(int position, MyGroupsBean.DataBean.NumberBean.AddBean bean);
    }
}
